<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: materialList.all_num }" :isShow="false" :isHaveExport="false" @toAdd="openDio">
      <template v-slot:table>
        <el-table v-loading="loading" element-loading-text="拼命加载中" ref="multipleTable" :data="materialList.data" stripe
            header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.9%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="35"></el-table-column>
          <el-table-column label="关键词" min-width="225">
            <template slot-scope="scope">{{ scope.row.keyword }}</template>
          </el-table-column>
          <el-table-column label="菜单类型" min-width="225">
            <template slot-scope="scope">{{
              isTypes(scope.row.types)
            }}</template>
          </el-table-column>

          <el-table-column label="匹配类型" min-width="260">
            <template slot-scope="scope">{{
              scope.row.is_like == 0 ? "精确匹配" : "模糊匹配"
            }}</template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="260">
            <template slot-scope="scope">{{
              scope.row.created_time | getTimeFormat
            }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
              <el-button :loading="$store.state.isLoading" size="mini" @click="deleteItems(scope.row.id)" type="danger">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>
  </div>
</template>
<script>
export default {
  data () {
    return {
      materialList: "",
      pageSize: 10,
      currentPage: 1,
      dioLoading: false,
      multipleSelection: [],
      formLabelWidth: "100px",
      loading: false
    };
  },
  methods: {
    openDio () {
      this.$store.commit("addbreadcrumbs", {
        title: "添加",
        url: `/index/wechat/addwxtext`
      });

      this.$store.commit("setUpdateWxText", "");
      this.$router.push("/index/wechat/addwxtext");
    },
    getMaterialList (page, page_size) {
      this.loading = true;
      page = page || 1;
      page_size = page_size || 10;
      var data = { page: page, page_size: page_size };
      this.$api.getWechat.getWxText(data).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          this.materialList = res.data.result;
        }
      });
      this.loading = false;
    },
    handleSizeChange (val) {
      this.getMaterialList("", val);
      this.pageSize = val;
    },
    handleCurrentChange (val) {
      this.currentPage = val;
      this.getMaterialList(val, this.pageSize);
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    deleteItems (id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id };
          this.$api.getWechat.deleteWxtext(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getMaterialList();
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },
    deleteLises () {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getWechat.deleteAllWxtext(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getMaterialList();
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
              }
            });
          }
        });
      }
    },
    showEditDialog (index, row) {
      this.$store.commit("addbreadcrumbs", {
        title: "编辑",
        url: `/index/wechat/addwxtext`
      });

      this.$store.commit("setUpdateWxText", row);
      this.$router.push({ path: "/index/wechat/addwxtext" });
    },
    isTypes (type) {
      if (type == 0) {
        return "文本";
      } else if (type == 1) {
        return "单条图文";
      } else if (type == 2) {
        return "多条图文";
      } else {
        return "图片";
      }
    }
  },
  mounted () {
    this.getMaterialList();
  },
  watch: {}
};
</script>
